package cn.bone.base.utils;

import org.apache.logging.log4j.core.util.datetime.FastDateFormat;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static String convert(Date date){
        if(date==null){
            return null;
        }

        //传入时间和当前时间相差的秒数
        long difference = ((new Date().getTime()) - date.getTime())/1000;
        if(difference<60){
            return "1分钟前";
        }else if(difference<60*60){
            long minute = difference/60;
            return minute+"分钟前";
        }else if(difference<60*60*24){
            long hour = difference/(60*60);
            return hour+"小时前";
        }else if(difference<60*60*24*2){
            return "昨天";
        }else if(difference<60*60*24*3){
            return "前天";
        }else {
            FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
            return dateFormat.format(date);
        }
    }

    public static String formaterYYYYMMddHHmmss(Date date){
        if(date==null){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String formaterYYYYMMdd(Date date){
        if(date==null){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        return format.format(date);
    }

    public static String formaterYYYYMMddHHmm(Date date){
        if(date==null){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static Date parseYYYYMMddHHmmss(String timeStr){
        if(StringUtil.isEmpty(timeStr)){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseYYYYMMddHHmm(String timeStr){
        if(StringUtil.isEmpty(timeStr)){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseYYYYMMdd(String timeStr){
        if(StringUtil.isEmpty(timeStr)){
            return null;
        }
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化时间 yyyy年MM月dd日
     */
    public static String formater4(Date date){
        org.apache.commons.lang3.time.FastDateFormat format = org.apache.commons.lang3.time.FastDateFormat.getInstance("yyyy 年 MM 月 dd 日");
        return format.format(date);
    }

    public static String convertMinute(Integer second){
        if(second==null){
            return null;
        }
        int h = 0;
        int m = 0;
        int s = 0;
        if(second>3600){
            h = second/3600;
        }
        second = second%3600;
        if(second>60){
            m = second/60;
        }
        second = second%60;
        s = second;
        StringBuffer sb = new StringBuffer();
        if(h>0){
            sb.append(h);
            sb.append("时");
        }
        if(m>0){
            sb.append(m);
            sb.append("分");
        }
        if(s>0){
            sb.append(s);
            sb.append("秒");
        }
        return sb.toString();
    }

    /**条件查询时结束日期加一天*/
    public static String amountDay(String endTime){
        if(StringUtil.isEmpty(endTime)){
            return null;
        }
        Date date = TimeUtil.parseYYYYMMdd(endTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String res = TimeUtil.formaterYYYYMMdd(calendar.getTime());
        return res;
    }
}
